//#include <iostream>
//
//using namespace std;
//
//int main()
//{
//    cout << "Hello world!" << endl;
//    return 0;
//}
// Maximum Flow- Edmonds-Karp Algorithm
// by Iraklis Karagkiozoglou <i.kar@windowslive.com>
/*#include <iostream>
#include <climits>
#define MAXN 100
using namespace std;
typedef struct node_t node_t;
typedef struct edge_t edge_t;

node_t nodes[MAXN];
int nedges[MAXN]={0};
int edmondskarp(int source, int sink, int n){
    int max = 0;
    while(true){
        int q[MAXN]={0}, mins[MAXN]={INT_MAX}, h=0,t=0,c,i,j,min=INT_MAX;
        edge_t *pre[MAXN]={NULL}, *u;
        q[t++]=source;
        while(t>h && pre[sink]==NULL){
            c = q[h++];
            for(i=0;i<n;i++) {
                if(nodes[c].edges[i].cap>0 && pre[nodes[c].edges[i].ni]==NULL){
                    q[t++]=nodes[c].edges[i].ni;
                    pre[nodes[c].edges[i].ni]=&nodes[c].edges[i];
                    if(mins[c]>nodes[c].edges[i].cap) mins[nodes[c].edges[i].ni]=nodes[c].edges[i].cap;
                    else mins[nodes[c].edges[i].ni]=mins[c];
                }
            }
        }
        if(pre[sink]==NULL) break;
	u=pre[sink];
	while(u!=NULL) {
            	(*u).cap-=mins[sink];
		u=pre[(*u).i];
	}
        max+=mins[sink];
    }
    return max;
}

int main(){
    int n, m, i, k, j, c,maxflow;
    cin >> n >> m;
    for(i=0;i<m;i++) {
        cin >> k >> j >> c;
        nodes[k].i=k;
        nodes[k].edges[nedges[k]].i=k;
        nodes[k].edges[nedges[k]].cap =c;
        nodes[k].edges[nedges[k]].ni=j;
        nedges[k]++;
    }
    maxflow=edmondskarp(0,n-1,n);
    cout << maxflow << endl;
    cin >> i;
    return 0;
}
 void graphAdjust();
        void graphAdjust2();
        int check(bool *c, int *p, int num);
        bool ford(int s, int t, int num, float &a, int *p);
        void fulkerson(int s, int t, float a, int *p);
        void ford_fulkerson(int s, int t, int num);

Graph(): _isFlowExisted(false);
        void readGraph();
        bool isFlowAvailable();
        void maxFlow();
        void minFlow()
        void printGraph();

        const static int MAXNODE = 100 ;
        struct node{
            float lower ;
            float upper ;
        } ;
        struct networkNode{
            float l, u ; // max volmun, min volumn ;
            float curr ; // current volumn ;
        } ;
        node _matrix[MAXNODE][MAXNODE] ; // Graph edge volumn array, if one edge's lower < 0
                                         // there is no edge from i to j ;
        float _newMatrix[MAXNODE+2][MAXNODE+2] ; // store new graph ;
        networkNode _flowMatrix[MAXNODE+1][MAXNODE+2] ; // for ford-fulckson algorithm
        int _s, _t ; // start and end point ;
        int _nodeNum, _edgeNum ;
        bool _isFlowExisted ; // return value of isFlowAvailable() ;

void Graph::graphAdjust(){
            // put another two nodes into the graph and re-arrange the volumn of edges;
            // _nodeNum+1 as new start node, _nodeNum+2 as new end node ;
            int tempNodeNum = _nodeNum+2 ;
            float tempVol, tempVolSum ;
            for ( int i = 0 ; i < tempNodeNum ; ++i )
                for ( int j = 0 ; j < tempNodeNum ; ++j ){
                    if ( i == _nodeNum && j < _nodeNum ){ // new start to other old nodes
                        tempVolSum = 0 ;
                        for ( int k = 0 ; k < _nodeNum ; ++k ){
                            tempVol = _matrix[k][j].lower ;
                            if ( tempVol != -1 )
                                tempVolSum += tempVol ;
                        }
                        _newMatrix[i][j] = tempVolSum ;
                    }
                    else
                    if ( j == _nodeNum+1 && i < _nodeNum ){
                        tempVolSum = 0 ;
                        for ( int k = 0 ; k < _nodeNum ; ++k ){
                            tempVol = _matrix[i][k].lower ;
                            if ( tempVol != -1 )
                                tempVolSum += tempVol ;
                        }
                        _newMatrix[i][j] = tempVolSum ;
                    }
                    else
                    if ( i < _nodeNum && j < _nodeNum ){
                        tempVol = _matrix[i][j].lower ;
                        _newMatrix[i][j] = ((tempVol == -1) ? -1 : (_matrix[i][j].upper-tempVol)) ;
                    }
                    else
                        _newMatrix[i][j] = -1 ;
                }
            //_s = _nodeNum ;
            //_t = _s+1 ;
            _newMatrix[_s][_t] = numeric_limits<float>::max() ;
            _newMatrix[_t][_s] = _newMatrix[_s][_t] ;
            return ;
        }*/
